package com.radiation.rms.job;

import com.alibaba.fastjson.JSON;
import com.radiation.rms.common.RMSConstant;
import com.radiation.rms.common.RMSDictionary;
import com.radiation.rms.entity.AbnormalData;
import com.radiation.rms.entity.RealTimeData;
import com.radiation.rms.mapper.DeviceMapper;
import com.radiation.rms.mapper.RealTimeDataMapper;
import com.radiation.rms.service.AbnormalDataService;
import com.radiation.rms.service.RealTimeDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author 10626
 */
@Component
@Slf4j
public class SchedulTask {

    @Autowired
    private RealTimeDataMapper realTimeDataMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private AbnormalDataService abnormalDataService;

    @Autowired
    private RealTimeDataService realTimeDataService;

    @Value("${socket.port}")
    private Integer socketPort;

    @Autowired
    private DeviceMapper deviceMapper;
    private List<Integer> macList = new ArrayList<Integer>();

    /**
     * 删除3个月数据
     */
    @Scheduled(cron = "0 0 10 ? * MON")
    public void deleteDate() {
        log.info("进入删除3个月数据");
        realTimeDataMapper.deleteDate();
    }

    /**
     * 五分钟没跑数据设置设备失效
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    @Transactional
    public void updateDevice() {
        log.info("进入查看设备失效");
        List<String> macInvalidList = new ArrayList<>();
        List<String> macNormalList = new ArrayList<>();
        List<String> macList = deviceMapper.getMac();
        for (String mac : macList) {
            Boolean aBoolean = redisTemplate.hasKey(RMSDictionary.deviceEnum.DEVICE_PREFIX.getCode() + mac);
            if (aBoolean == null || !aBoolean) {
                macInvalidList.add(mac);
            } else {
                macNormalList.add(mac);
            }
        }
        log.info("生效的设备值:{}", macNormalList.toString());
        log.info("异常的设备值:{}", macInvalidList.toString());
        if (macInvalidList.size() > 0) {
            deviceMapper.updateMacStatus(new Date(), RMSDictionary.deviceEnum.DEVICE_ABNORMAL.getCode(), macInvalidList);
        }
        if (macNormalList.size() > 0) {
            deviceMapper.updateMacStatus(new Date(), RMSDictionary.deviceEnum.DEVICE_NORMAL.getCode(), macNormalList);
        }
    }

    @Scheduled(cron = "* * * * * ?")
    public void schedule() {
        log.debug("开始创造假数据");
        Random ra = new Random();
        for (int i = 0; i < 10; i++) {
            int mac = ra.nextInt(110) + 1;
            if (macList.contains(mac)) {
                log.info("重复数据");
                return;
            } else {
                macList.add(mac);
                if (macList.size() >= 10) {
                    macList.clear();
                }
            }
            if (socketPort.equals(8085)) {
                try {
                    Thread.sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            int data = ra.nextInt(26) % (26 - 16 + 1) + 16;
            double record = data / 100.00;
            redisTemplate.opsForValue().set(RMSDictionary.deviceEnum.DEVICE_PREFIX.getCode() + mac, record + "", 10 * 60 * 1000, TimeUnit.MILLISECONDS);
            RealTimeData realTimeData = new RealTimeData();
            realTimeData.setRecord(record);
            realTimeData.setMac(mac + "");
            realTimeData.setGmtCreate(new Date());
            realTimeData.setGmtModified(new Date());
            realTimeData.setIsDeleted(false);
            realTimeData.setStatus(RMSConstant.NORMAL);

            if (record > 2.5) {
                AbnormalData abnormalData = new AbnormalData();
                abnormalData.setRecord(record);
                abnormalData.setMac(mac + "");
                abnormalDataService.save(abnormalData);
                realTimeData.setStatus(RMSConstant.ABNORMAL);
            }
            realTimeDataService.save(realTimeData);
            BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps("device:info");
            hashOps.put(mac + "", JSON.toJSONString(realTimeData));
            log.info("成功");

        }
    }
}
